<?php

class Membr_Controller extends CI_Controller {

    public $layout = "layouts/member";
    public $_site_path;

    public function __construct() {
    	$this->_site_path = config_item('front_path');
    	date_default_timezone_set('PRC');
        parent::__construct();
        //检查登陆
        if (!$this->is_login()) {
        	$this->message_js("请先登录，谢谢!", base_url($this->_site_path . '/member_do/login'));
        }
    }
    
    //判断用户是否已登陆
    private function is_login(){
    	 
    	$user_id = $this->session->userdata('user_id');
    	$user_name = $this->session->userdata('user_name');
    	if($user_id && $user_name){
    		//对比地址来源
    		if(isset($_SERVER['HTTP_REFERER'])){
    			$url = parse_url($_SERVER['HTTP_REFERER']);
    			if($_SERVER['HTTP_HOST'] != $url["host"]){
    				return false;
    			}
    		}
    		$this->load->model('member_model');
    		$tmp_name = $this->member_model->get_value_by_pk($user_id, 'user_name');
    		return $user_name==$tmp_name ? true : false;
    	}else{
    		return false;
    	}
    }

    /**
     * 用js报错
     * @param string $value 要输出的值
     */
    public function error_js($value) {
        header("Content-type:text/html;charset=utf-8");
        echo "<script>alert('" . $value . "');history.go(-1)</script>";
    }

    /**
     * js提示消息盒子
     * @param type $value  提示的值
     * @param type $url  跳转的路径
     */
    public function message_js($value, $url) {
        header("Content-type:text/html;charset=utf-8");
        echo "<script>alert('" . $value . "');window.location.href='" . $url . "';</script>";
    }

    /**
     * js页面跳转
     * @param  string $url 跳转路径
     * @return null
     */
    public function redirect_location($url) {
        header("Content-type:text/html;charset=utf-8");
        echo "<script>window.location.href='" . $url . "';</script>";
    }

}

